// pages/market/orderConfirm/orderConfirm.js
var app = getApp(); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: {
      id: 9,
      img: 'http://www.meiliancheng.cn/public/meiliancheng/imagezy/banner.png',
      imgUrls: [
        'http://www.meiliancheng.cn/public/meiliancheng/imagezy/banner.png',
        'http://www.meiliancheng.cn/public/meiliancheng/imagezy/banner.png',
        'http://www.meiliancheng.cn/public/meiliancheng/imagezy/banner.png'
      ],
      title: '商品标题商品标题商品标题',
      tips: '商品描述 商品描述',
      price: 34,
      integral: 220,
      nums:2,
    },
    dialogShow:false,
  },
  confirmBtn:function(){
    var that = this
    wx.showModal({
      title: '确认兑换',
      content: '确定兑换该产品？',
      success(res){
        if(res.confirm){
          wx.request({
            url: app.siteInfo.scoreroot + 'addorder',
            method: "get",
            data: {
              id: that.data.id,
              num: that.data.exchangeNums,
              beizhu: that.data.beizhu,
              uid: app.siteInfo.uid,
              pid: app.siteInfo.pid,
              com_part_id: app.siteInfo.com_part_id,
            },
            success: function (res) {
              if (res.data.code == 200) {
                that.setData({
                  dialogShow: true,
                })
              } else if(res.data.code == 201){
                wx.request({
                  url: app.siteInfo.scoreroot + 'payorder',
                  data: {
                    orderid: res.data.orderid,
                    pid: app.siteInfo.pid,
                    uid: app.siteInfo.uid,
                    com_part_id: app.siteInfo.com_part_id
                  },
                  method: 'post',
                  success: function (res) {
                    wx.requestPayment({
                      'appId': res.data.appId,
                      'timeStamp': res.data.timeStamp,
                      'nonceStr': res.data.nonceStr,
                      'package': res.data.package,
                      'signType': res.data.signType,
                      'paySign': res.data.paySign,
                      'success': function (res) {
                        console.log(res);

                      },
                      'fail': function (res) {
                        console.log(res);
                      },
                      complete: function () {
                        wx.redirectTo({
                          url: '/pages/market/market',
                        })
                      }
                    })
                  }
                })
              } else if(res.data.code == 501){
                wx.showToast({
                  title: '积分不足',
                  icon: 'none',
                })
              }else {
                wx.showToast({
                  title: '领取失败',
                  icon: 'none',
                })
              }
            }
          })
          
        }
      }
    })
  },
  addbeizhu:function(e){
    that.setData({
      beizhu: e.detail.value
    })
  },
  wozhidaole:function(){
    var that = this;
    that.setData({
      dialogShow: false,
    })
    wx.redirectTo({
      url: '/pages/market/market',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    var nums = options.nums;
    that.setData({
      exchangeNums:nums,
      id:id
    })
    wx.request({
      url: app.siteInfo.scoreroot + 'orderconfirm',
      method: "get",
      data: {
        id: id,
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        com_part_id: app.siteInfo.com_part_id,
      },
      success: function (res) {
        
        that.setData({
          product: res.data.list,
          info:res.data.info
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '兑换确认'//页面标题为路由参数
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})